﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EShop.BLL;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmShopHotMerchandise : frmBase
    {
        public frmShopHotMerchandise()
        {
            InitializeComponent();
        }

        private void frmShopHotMerchandise_Load(object sender, EventArgs e)
        {
            cbxIsTop.Checked = true;
        }

        private void GetHot()
        {
            ShopReportBLL oShopReportBLL = new ShopReportBLL();
            DataTable dt = oShopReportBLL.GetHotMerchandise(dtBegin.Value, dtEnd.Value);
            int iRows = dt.Rows.Count;
            int iTop = 0;
            int iSelledQuantity = 0;
            if (iRows < 1) return;
            if (cbxIsTop.Checked&&iRows>10) iTop = 10;
            else iTop = iRows;
            for (int i = 0; i < iRows; i++)
            {
                iSelledQuantity += (int)dt.Rows[i]["Buynumber"];
            }
            dgvResult.Rows.Clear();
            int index = dgvResult.Rows.Add();
            for (int i = 0; i < iTop; i++)
            {
                index = dgvResult.Rows.Add();
                decimal mRate = 0;
                object sellQty = dt.Rows[i]["Buynumber"];
                mRate = Convert.ToDecimal(sellQty) / Convert.ToDecimal(iSelledQuantity);
                mRate = mRate * 100;
                dgvResult.Rows[index].Cells["Barcode"].Value = dt.Rows[i]["Barcode"];
                dgvResult.Rows[index].Cells["MerchandiseName"].Value = dt.Rows[i]["MerchandiseName"];
                dgvResult.Rows[index].Cells["Buynumber"].Value = sellQty;
                dgvResult.Rows[index].Cells["Rate"].Value = FormatManager.FormatDecimal(mRate) + "%";
            }
            dgvResult.Rows[0].Cells[0].Value = "总销售量:";
            dgvResult.Rows[0].Cells[1].Value = iSelledQuantity;
        }

        private void DateChanged()
        {
            if (this.dtBegin.Value > dtEnd.Value) return;
            GetHot();
        }

        private void dtBegin_ValueChanged(object sender, EventArgs e)
        {
            DateChanged();
        }

        private void dtEnd_ValueChanged(object sender, EventArgs e)
        {
            DateChanged();
        }

        private void cbxIsTop_CheckedChanged(object sender, EventArgs e)
        {
            DateChanged();
        }


    }
}
